/*****************************************************************************
 * 
 *  File:  uartmgr.h
 *  -----
 * 
 *  Copyright Statement:
 *  --------------------
 *  This software is protected by Copyright and the information contained
 *  herein is confidential. The software may not be copied and the information
 *  contained herein may not be used or disclosed except with the written

 *  (C) Hisi. 2004-2005.
 * 
 *  Project:  
 *  --------
 * 
 *  Description:
 *  ------------
 *  This header file contains the UART manager definitions 
 * 
 *  Modification History:
 *  ---------------------
 *  Date        Version         Author        		 Details
 *  ----        -------        ------         		-------
 *  Dec.15      1.1           Huang Yonghua       	  Original
 *
 *****************************************************************************/

#ifndef _HI6810_UARTMGR_INC 
#define _HI6810_UARTMGR_INC 
#include "uartdrv.h"


/*
 * DEFINITIONS
 *****************************************************************************/

// Boot ROM commands
#define CMD_CHAR_BR_WAITCON               ( ( unsigned char ) 0x44 ) //'D'
#define CMD_CHAR_BR_CHANGERATEUART        ( ( unsigned char ) 0x52 ) //'R'
#define CMD_CHAR_BR_TIMEOUT               ( ( unsigned char ) 0x54 ) //'T'
#define CMD_CHAR_BR_SETADD                ( ( unsigned char ) 0x53 ) //'S'
#define CMD_CHAR_BR_HWORDLOAD             ( ( unsigned char ) 0x48 ) //'H'
#define CMD_CHAR_BR_LOAD                  ( ( unsigned char ) 0x4C ) //'L'
#define CMD_CHAR_BR_GET                   ( ( unsigned char ) 0x47 ) //'G'
#define CMD_CHAR_BR_WAITVAL               ( ( unsigned char ) 0x57 ) //'W'
#define CMD_CHAR_BR_JUMP                  ( ( unsigned char ) 0x4A ) //'J'
#define CMD_CHAR_BR_EXEC                  ( ( unsigned char ) 0x45 ) //'E'
#define CMD_CHAR_BR_GETQUERYDATA          ( ( unsigned char ) 0x51 ) //'Q'
#define CMD_CHAR_BR_HOSTDONGLERESP        ( ( unsigned char ) 0x44 ) //'D'
#define CMD_CHAR_BR_CALCSIGNATURE         ( ( unsigned char ) 0x43 ) //'C'
#define CMD_CHAR_BR_READPRODUCTID         ( ( unsigned char ) 0x50 ) //'P'
#define CMD_CHAR_BR_CHANGEUARTCONFIG      ( ( unsigned char ) 0x54 ) //'T'
#define CMD_CHAR_BR_BURNOTP               ( ( unsigned char ) 0x42 ) //'B'
#define CMD_CHAR_BR_CHANGEPARITYSTOPUART  ( ( unsigned char ) 0x55 ) //'U'
#define CMD_CHAR_BR_READVERSION           ( ( unsigned char ) 0x56 ) //'V'



// Loader commands
#define CMD_CHAR_LD_CHANGERATEUART    ( ( unsigned char ) 0x52 ) //'R'
#define CMD_CHAR_LD_EXEC              ( ( unsigned char ) 0x4A ) //'J'
#define CMD_CHAR_LD_COLDRESET         ( ( unsigned char ) 0x43 ) //'C'
#define CMD_CHAR_LD_SETADD            ( ( unsigned char ) 0x53 ) //'S'
#define CMD_CHAR_LD_GETMEMORYDEVICE   ( ( unsigned char ) 0x44 ) //'D'
#define CMD_CHAR_LD_ERASE             ( ( unsigned char ) 0x45 ) //'E'
#define CMD_CHAR_LD_LOAD              ( ( unsigned char ) 0x4C ) //'L'
#define CMD_CHAR_LD_GET               ( ( unsigned char ) 0x47 ) //'G'
#define CMD_CHAR_LD_CHANGEUARTCONFIG  ( ( unsigned char ) 0x54 ) //'T'
#define CMD_CHAR_LD_USERDEFINE        ( ( unsigned char ) 0x55 ) //'U'
#define CMD_CHAR_LD_READLOADERVERSION ( ( unsigned char ) 0x56 ) //'V'


// New command for Loader add during USB and Nand Flash integration
#define CMD_CHAR_LD_FLASHLASTDATA     ( ( unsigned char ) 0x46 ) //'F'
#define CMD_CHAR_LD_INITIAL_IBT       ( ( unsigned char ) 0x49 ) //'I'
#define CMD_CHAR_LD_WRITE_IBT         ( ( unsigned char ) 0x57 ) //'W'
#define CMD_CHAR_LD_GET_FLASH_PARAM ( ( unsigned char ) 0x50 ) //'P'
#define CMD_CHAR_LD_GET_FIRST_BLOCK        ( ( unsigned char ) 0x42 ) //'B'
#define CMD_CHAR_LD_ERASE_ONE_BLOCK        ( ( unsigned char ) 0x4F ) //'O'
#define CMD_CHAR_LD_NAND_SETADD       ( ( unsigned char ) 0x4E ) //'N'
#define CMD_CHAR_LD_VERIFY           ( ( unsigned char ) 0x3D ) //'='
#define CMD_CHAR_LD_PARTITION_RAW    ( ( unsigned char ) 0x41 ) //'A'

// Boot ROM responses
#define CMD_CHAR_BR_RESP_CREQ         ( ( unsigned char ) 0x81 ) //Creq
#define CMD_CHAR_BR_RESP_CACK         ( ( unsigned char ) 0x44 ) //'D'
#define CMD_CHAR_BR_RESP_OK           ( ( unsigned char ) 0x4F ) //'O'
#define CMD_CHAR_BR_RESP_KO           ( ( unsigned char ) 0x4B ) //'K'

// Boot ROM responses
#define CMD_CHAR_LD_RESP_TIMEOUT_ERROR  ( ( unsigned char ) 0x54 ) //'T'
#define CMD_CHAR_LD_RESP_CHEKSUM_ERROR  ( ( unsigned char ) 0x43 ) //'C'


#define BFLOADERDLL_TIMEOUT_CACK      1000  // 1 second

#define BFLOADERDLL_CMD_SIZE          1  // 1 Header byte
#define BFLOADERDLL_LENGTH_SIZE       1  // 1 Size byte
#define BFLOADERDLL_CRC_SIZE          1  // 1 CRC byte

#define BFLOADERDLL_RAM_TRANSFERT_SIZE                        512    /* nb bytes */
#define BFLOADERDLL_NOR_FLASH_TRANSFERT_SIZE                  64    /* nb bytes */
#if UART_STD_DRV_EN
#define BFLOADERDLL_UART_NOR_FLASH_TRANSFERT_SIZE            1024    /* nb bytes */
#else
#define BFLOADERDLL_UART_NOR_FLASH_TRANSFERT_SIZE           2048
#endif
#define BFLOADERDLL_UART_FRAME_SIZE			                      0x10	// 64 bytes

#define BFLOADERDLL_UART_UPLOAD_FRAME_SIZE			      512	// 2048 bytes

#define BFLOADERDLL_OVERHEAD ( BFLOADERDLL_CMD_SIZE + BFLOADERDLL_LENGTH_SIZE + BFLOADERDLL_CRC_SIZE )		

// UART mode configuration
#define UART_ONE_STP_BIT                    0x0
#define UART_TWO_STP_BIT                    0x4
#define UART_ENABLE_PARITY                  0x8
#define UART_PARITY_POLARITY                0x10
#define UART_STICK_PARITY                   0x20
#define UART_NO_PARITY                      0x0
#define UART_ODD_PARITY                     (UART_ENABLE_PARITY)
#define UART_EVEN_PARITY                    (UART_ENABLE_PARITY+UART_PARITY_POLARITY)
#define UART_STICK_PARITY_ONE               (UART_STICK_PARITY  + UART_ODD_PARITY)
#define UART_STICK_PARITY_ZERO              (UART_STICK_PARITY  + UART_EVEN_PARITY)


/*
 * TYPE DEFINITIONS
 *****************************************************************************/

typedef struct
{
  int     nbBlock;
  int     nBlockSize;
  int     nPageSize;
  int     nSpareArraySize;
  int     nEraseTime;
  int     nWriteTime;

} BFLOADER_FLASH_PARAM_T;

typedef enum
{
	// Not used
	TARGET_NOTYPE = 0,

	// Supported Target Type
	TARGET_PCF5213_3C,
	TARGET_PCF5213_3E,

    // SySol5210
	TARGET_PNX5230,
	TARGET_PNX5230_2A,
	TARGET_PNX5230_3A_ROM4,

    // SySol7210
	TARGET_PNX5220,
	TARGET_PNX5220_2A,
	TARGET_PNX5220_2A_ROM3,
	TARGET_PNX5220_2B_ROM4,
	TARGET_PNX5220_3A_ROM5,

  // Sysol 
	TARGET_PNX5217,

    // Sysol6511
	TARGET_PNX6511_1A_ROM1,

	// Last Target type
	TARGET_LASTTYPE

} BFLOADER_TARGET_TYPE_T;


typedef enum
{
	// Not used
	CMD_NOCOMMAND = 0,
	
	// Boot ROM commands
	CMD_BR_WAITCON,
	CMD_BR_CHANGERATEUART,
	CMD_BR_TIMEOUT,
	CMD_BR_SETADD,
	CMD_BR_HWORDLOAD,
	CMD_BR_LOAD,
	CMD_BR_GET,
	CMD_BR_WAITVAL,
	CMD_BR_JUMP,
	CMD_BR_EXEC,
	CMD_BR_GETQUERYDATA,
	CMD_BR_HOSTDONGLERESP,
	CMD_BR_CALCSIGNATURE,
	CMD_BR_READPRODUCTID,
	CMD_BR_CHANGEUARTCONFIG,
	
	// Loader commands
	CMD_LD_CHANGERATEUART,
	CMD_LD_EXEC,
	CMD_LD_COLDRESET,
	CMD_LD_SETADD,
	CMD_LD_GETMEMORYDEVICE,
	CMD_LD_ERASE,
	CMD_LD_LOAD,
	CMD_LD_GET,
	CMD_LD_CHANGEUARTCONFIG,
	CMD_LD_USERDEFINE,
	CMD_LD_READLOADERVERSION,
	
	// New loader command add during USB NAND FLASH integration
	CMD_LD_FLASHLASTDATA,
	CMD_LD_INITIAL_IBT,
	CMD_LD_WRITE_IBT,
	CMD_LD_GET_FLASH_PARAM,
	CMD_LD_GET_FIRST_BLOCK,
	CMD_LD_ERASE_ONE_BLOCK,
	CMD_LD_NAND_SETADD,
	CMD_LD_VERIFY,
	CMD_LD_PARTITION_RAW,
	
	// New commands must be add here
	CMD_BR_BURNOTP,
	CMD_BR_CHANGEPARITYSTOPUART,
	CMD_BR_READVERSION,
	
	// Last command
	CMD_LASTCOMMAND

} BFLOADER_COM_CMD_TYPE_T;

/*
typedef enum
{
	COM_NOPORT = 0,
	COM_PORT1,
	COM_PORT2,
	COM_PORT3,
	COM_PORT4,
	COM_PORT5,
	COM_PORT6,	
	COM_PORT7,
	COM_PORT8
} BFLOADER_UART_COM_PORT_T;*/
/*
typedef enum
{
	COM_BR9600 = 0,
	COM_BR19200,
	COM_BR38400,
	COM_BR57600,
	COM_BR115200,
	COM_BR230400,
	COM_BR460800,
	COM_BR921600

} BFLOADER_UART_BAUD_RATE_T;*/

typedef enum
{
	COM_DATABIT5 = 0,
	COM_DATABIT6,
	COM_DATABIT7,
	COM_DATABIT8

} BFLOADER_UART_DATA_BIT_T;

typedef enum
{
	COM_NOPARITY = 0,
	COM_ODDPARITY,
	COM_EVENPARITY

} BFLOADER_UART_PARITY_T;

typedef enum
{
	COM_1STOPBIT = 0,
	COM_15STOPBIT,
	COM_2STOPBIT

} BFLOADER_UART_STOP_BIT_T;

typedef enum
{
	COM_NOCTRLFLOW = 0,
	COM_XONXOFFCTRLFLOW,
	COM_HARDCTRLFLOW

} BFLOADER_UART_CTRL_FLOW_T;

typedef struct  crcTag 
{
    unsigned long int CRC32tab[256];
    unsigned long int  CRC_val;
    unsigned long int CRCSeed;
    unsigned char *CRC_buffer;

}crc;

/*****************************************************************************
 *
 * Function:          BFLOADER_ConfigInitDialog()
 *
 *
 * Description:       Initialize the communication media with the target
 *				
 *	
 *
 * Input parameters:  n_TargetType  Target type
 *                    pt_ComConfig  Device configuration
 *
 * Output parameters: pul_ComDevice Device identifier
 *
 * Return:            int
 *
 *****************************************************************************/
BFLOADER_COM_ERROR_CODE_T   BFLOADER_ComConfigInitDialog( int n_TargetType, const BFLOADER_COM_CONFIG_T *pt_ComConfig, 
			    	const unsigned long *pul_ComDevice );


/*****************************************************************************
 *
 * Function:          BFLOADER_ComChangeConfig()
 *
 *
 * Description:       Change the communication configuration with the target
 *				
 *	
 *
 * Input parameters:  pul_ComDevice   Device identifier
 *                    pt_ComConfig    Device configuration
 *
 * Output parameters: void
 *
 * Return:            int
 *
 *****************************************************************************/
BFLOADER_COM_ERROR_CODE_T   BFLOADER_ComChangeConfig( const unsigned long *pul_ComDevice,const  BFLOADER_COM_CONFIG_T *pt_ComConfig );


/*****************************************************************************
 *
 * Function:          BFLOADER_ComCloseDialog()
 *
 *
 * Description:       Close the communication media with the target
 *				
 *	
 *
 * Input parameters:  pul_ComDevice   Device identifier
 *
 * Output parameters: void
 *
 * Return:            int
 *
 *****************************************************************************/
BFLOADER_COM_ERROR_CODE_T   BFLOADER_ComCloseDialog(const  unsigned long *pul_ComDevice );


/*****************************************************************************
 *
 * Function:          BFLOADER_ComSendCmd()
 *
 *
 * Description:       Send a command to the target and wait for an acknowledgement
 *				
 *	
 * Input parameters:  pul_ComDevice Device identifier
 *                    n_CmdType     Command to send
 *                    n_InSize      Number of bytes to transmit from tx buffer
 *                    puc_CmdData   Tx buffer - must be at least of size n_InSize
 *                    n_OutSize     Maximum number of bytes to receive
 *                    n_Timeout     Timeout for the command  (in ms)
 *
 * Output parameters: puc_RcvData   RX buffer - must be at least of size n_OutSize
 *                    pn_RcvSize    Number of bytes effectively received
 *
 * Return:            int
 *
 *****************************************************************************/
BFLOADER_COM_ERROR_CODE_T   BFLOADER_ComSendCmd( const unsigned long *pul_ComDevice, int n_CmdType, int n_InSize, unsigned char *puc_CmdData,
				int n_OutSize, int n_Timeout, unsigned char *puc_RcvData, int *pn_RcvSize );

BFLOADER_COM_ERROR_CODE_T ComMgr_WaitData( int n_OutSize, int n_Timeout, unsigned char *puc_RcvData, int *pn_RcvSize );
BFLOADER_COM_ERROR_CODE_T ComMgr_GetEraseMsg( void );
extern void Delay(UINT32 count);
char*   BFLOADER_GetDllVersion ( );
#endif/* _HI6810_UARTMGR_INC */
